package com.mls.medicinereminder.controller;


import com.mls.medicinereminder.entity.AvoidWarnDetails;
import com.mls.medicinereminder.entity.Medicine;
import com.mls.medicinereminder.service.ReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
public class ReminderController {

    @Autowired
    ReminderService reminderService;

    @PostMapping(value = "/avoids",consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<ArrayList<Medicine>> isAvoid(@RequestBody ArrayList<Medicine> medicines){

                ArrayList<Medicine> medicinesResult=new ArrayList<Medicine>();
                 Medicine medicineRecord=new Medicine();
                for(Medicine medicine:medicines){
                    medicineRecord=null;
                    medicineRecord=reminderService.isAvoid(medicine.getName());
                    if(medicineRecord!=null){
                        medicinesResult.add(medicineRecord);
                    }
                }
                if(medicinesResult.size()==0){
                    return new ResponseEntity<>(HttpStatus.OK);
                }
                else return new ResponseEntity<ArrayList<Medicine>>(medicinesResult,HttpStatus.ACCEPTED);

    }

    @PostMapping(value = "/warns",consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<ArrayList<Medicine>> isWarn(@RequestBody ArrayList<Medicine> medicines){
        ArrayList<Medicine> medicinesResult=new ArrayList<Medicine>();
        Medicine medicineRecord=new Medicine();
        for(Medicine medicine:medicines){
            medicineRecord=null;
            medicineRecord=reminderService.isWarn(medicine.getName());
            if(medicineRecord!=null){
                   medicinesResult.add(medicineRecord);
            }
        }
        if(medicinesResult.size()!=0){
            return new ResponseEntity<ArrayList<Medicine>>(medicinesResult,HttpStatus.ACCEPTED);
        }
        else return new ResponseEntity<>(HttpStatus.OK);

    }

    @PostMapping(value = "/conflicts",consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<ArrayList<String>> isConflict(@RequestBody ArrayList<Medicine> medicines){
        ArrayList<String> conflicts=new ArrayList<String>();
        String name1;
        String name2;
        for(int i=0;i<medicines.size();i++){
//            System.out.println(i);
            name1=medicines.get(i).getName();
            for(int j=i+1;j<medicines.size();j++){
//                System.out.print(" "+j);
                name2=medicines.get(j).getName();
                if(reminderService.isConflict(name1,name2)){
                    conflicts.add(name1+"与"+name2+"不能联用");
                }
            }
        }
        if(conflicts.size()!=0){
            return new ResponseEntity<ArrayList<String>>(conflicts,HttpStatus.ACCEPTED);
        }
        else return new ResponseEntity<>(HttpStatus.OK);
    }

}
